﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebCommit.Common.Entitys;
using WebCommit.Common.Interfaces;
using WebCommit.Common.Factories;
using WebCommit.Web.Models;
using WebCommit.Common.Mvc.BaseClass;
using WebCommit.Common.Models;

namespace WebCommit.Web.Controllers
{
	/// <summary>
	/// 用户访问的控制器
	/// </summary>
	public class UserVisController : BaseController
    {
        //
        // GET: /Header/
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Login(LoginRequest model)
		{
			UserEntity user = SvcFactory.GetInstance<IUserSvc>().Login(model.UserName, model.Password);
			if (user != null)
			{
				if (user.UserStatus == Common.Enums.UserStatus.Normal)
				{
					HttpCookie userName = new HttpCookie("UserName", model.UserName) { Expires = DateTime.Now + new TimeSpan(365, 0, 0, 0) };
					HttpCookie autologin = new HttpCookie("AutoLogin", model.AutoLogin.ToString()) { Expires = DateTime.Now + new TimeSpan(365, 0, 0, 0) };
					if (model.AutoLogin)
					{
						HttpCookie password = new HttpCookie("Password", model.Password) { Expires = DateTime.Now + new TimeSpan(365, 0, 0, 0) };
						Response.Cookies.Add(password);
					}
					Response.Cookies.Add(userName);
					Response.Cookies.Add(autologin);
					LoginUser = user;
					return Redirect(model.RedirctUrl);
				}
				else if (user.UserStatus == Common.Enums.UserStatus.Pending)
				{
					return RedirectToAction("ReValidate", "UserMgr", new { ValidateStatus = "NeedValidate",Email=user.Email,UserId=user.Id });
				}
				else
				{
					return RedirectToAction("NotAllow", "UserMgr", new { Email = user.Email, Id = user.Id });
				}
			}
			else
			{
				model.IsActLogin = true;
				model.Password = "";

				return RedirectToAction("Login", "UserMgr", model);
			}
        }


		//
		// GET: /Header/
		[ChildActionOnly]
		public ActionResult Login()
		{
			if (LoginUser != null)
				return PartialView();

			bool autoLogin = false;
			string userName = "", password = "";
			if (Request.Cookies["AutoLogin"] != null)
				autoLogin = string.IsNullOrEmpty(Request.Cookies["AutoLogin"].Value) ? false : bool.Parse(Request.Cookies["AutoLogin"].Value);
			if (Request.Cookies["UserName"] != null)
				userName = Request.Cookies["UserName"].Value;
			if (Request.Cookies["Password"] != null)
				password = Request.Cookies["Password"].Value;
			if (autoLogin && !string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
			{
				UserEntity user = SvcFactory.GetInstance<IUserSvc>().Login(userName, password);
				if (user != null)
				{
					if (user.UserStatus == Common.Enums.UserStatus.Normal)
					{ 
						return PartialView();					
					}
					else if (user.UserStatus == Common.Enums.UserStatus.Pending)
					{
						return RedirectToAction("ReValidate", "UserMgr", new { ValidateStatus = "NeedValidate", Email = user.Email, UserId = user.Id });
					}
					else
					{
						return RedirectToAction("NotAllow", "UserMgr", new { Email = user.Email, Id = user.Id });
					}

				}
				else
				{
					Response.Cookies["Password"].Value = null;
					Response.Cookies["AutoLogin"].Value = null;
				}
			}
			return Login2();
			
		}

		[ChildActionOnly]
		public PartialViewResult Login2()
		{
			LoginRequest model = new LoginRequest();
			model.UserName = Request["UserName"];
			model.RedirctUrl = Request.QueryString["RedirctUrl"];
			model.AutoLogin =string.IsNullOrEmpty(Request["AutoLogin"])?false: bool.Parse(Request["AutoLogin"]);
			return PartialView(model);
		}

		
		public ActionResult Logout()
		{
			LoginUser = null;
			Response.Cookies["Password"].Value = null;
			Response.Cookies["AutoLogin"].Value = null;
			return RedirectToAction("Login","UserMgr");
		}

		public ActionResult UserAttach()
		{
			if (LoginUser == null)
				return Json(null, JsonRequestBehavior.AllowGet);

			UserAttachEntity userAttach = SvcFactory.GetInstance<IUserSvc>().GetUserAttach(((UserEntity)LoginUser).Id);
			return Json(userAttach, JsonRequestBehavior.AllowGet);
		}
    }
}
